package com.huaji.learn.helper;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huaji.learn.R;
import com.huaji.learn.model.NoteModel;

import java.util.List;

/**
 * 笔记列表的适配器
 */
public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.ViewHolder> {

    /**
     * 数据列表
     */
    private static List<NoteModel> itemList;

    /**
     * 设置数据
     * @param itemList 要塞进列表的数据集合
     */
    public void setItems(List<NoteModel> itemList) {
        this.itemList = itemList;
        // 通知数据更改
        notifyDataSetChanged();
    }

    /**
     * 重写生命周期方法
     * @param parent The ViewGroup into which the new View will be added after it is bound to
     *               an adapter position.
     * @param viewType The view type of the new View.
     *
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_note, parent, false);
        return new ViewHolder(view);
    }

    /**
     * 生命周期方法，绑定数据到控件
     * @param holder The ViewHolder which should be updated to represent the contents of the
     *        item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NoteModel itemData = itemList.get(position);
        holder.textViewDate.setText(itemData.getCreateTime());
        holder.textViewType.setText(itemData.getNoteType());
    }

    /**
     * 获取列表的数据数量
     * @return 数量
     */
    @Override
    public int getItemCount() {
        return itemList.size();
    }

    /**
     * 列表项点击事件接口
     */
    public interface OnItemClickListener {
        /**
         * 列表项点击事件
         * @param note 点击的列表项所绑定的Model
         */
        void onItemClick(NoteModel note);
    }

    /**
     * 列表项点击事件 对外的
     */
    private static OnItemClickListener onItemClickListener;

    /**
     * 设置列表项点击事件
     * @param listener 监听器
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    /**
     * ViewHolder
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textViewDate;
        public TextView textViewType;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewDate = itemView.findViewById(R.id.listTextViewDate);
            textViewType = itemView.findViewById(R.id.listTextViewType);

            // 为 itemView 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取当前项的位置
                    int position = getAdapterPosition();

                    // 确保位置有效，避免越界
                    if (position != RecyclerView.NO_POSITION && onItemClickListener != null) {
                        // 调用接口中的方法，传递点击的笔记对象
                        onItemClickListener.onItemClick(itemList.get(position));
                    }
                }
            });
        }
    }
}
